package com.sxfq.loansupermarket.backsystem.modular.refunds.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.arrival.service.ICustomerService;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.refunds.dao.PaymentRecordDao;
import com.sxfq.loansupermarket.backsystem.modular.refunds.entity.PaymentRecordInfo;
import com.sxfq.loansupermarket.backsystem.modular.refunds.entity.SearchPaymentRecordInfo;
import com.sxfq.loansupermarket.backsystem.modular.refunds.service.IPaymentRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class PaymentRecordServiceImpl implements IPaymentRecordService {
    @Autowired
    private PaymentRecordDao dao;

    @Autowired
    ICustomerService customerService;

    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<PaymentRecordInfo> selectPageListInfo(Page<PaymentRecordInfo> page, SearchPaymentRecordInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getCustomerName())) {
            params.put("customerName", searchInfo.getCustomerName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerPhone())) {
            params.put("customerPhone", searchInfo.getCustomerPhone());
        }
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCheckoutMethod())) {
            String checkoutMethod = "";
            String checkoutMethods[] = searchInfo.getCheckoutMethod().split(",");
            if (checkoutMethods.length == 1) {
                checkoutMethod = checkoutMethods[0];
            } else {
                for (int i = 0; i < checkoutMethods.length; i++) {
                    if (i == (checkoutMethods.length - 1)) {
                        checkoutMethod += "'" + checkoutMethods[i];
                    } else if (i == 0) {
                        checkoutMethod += checkoutMethods[i] + "'" + ",";
                    } else {
                        checkoutMethod += "'" + checkoutMethods[i] + "'" + ",";
                    }
                }
            }
            params.put("checkoutMethod", " t1.checkout_method in ('" + checkoutMethod + "')");
        }
        if (StringUtil.isNotEmpty(searchInfo.getFieldTc())) {
            String fieldTc = "";
            String fieldTcs[] = searchInfo.getFieldTc().split(",");
            if (fieldTcs.length == 1) {
                fieldTc = fieldTcs[0];
            } else {
                for (int i = 0; i < fieldTcs.length; i++) {
                    if (i == (fieldTcs.length - 1)) {
                        fieldTc += "'" + fieldTcs[i];
                    } else if (i == 0) {
                        fieldTc += fieldTcs[i] + "'" + ",";
                    } else {
                        fieldTc += "'" + fieldTcs[i] + "'" + ",";
                    }
                }
            }
            params.put("fieldTc", " (t1.tc_name in ('" + fieldTc + "') or d.tc_name in ('" + fieldTc + "'))");
        }
        if (StringUtil.isNotEmpty(searchInfo.getDoctor())) {
            params.put("doctor", searchInfo.getDoctor());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerStatus())) {
            String customerStatus = "";
            String customerStatuss[] = searchInfo.getCustomerStatus().split(",");
            if (customerStatuss.length == 1) {
                customerStatus = customerStatuss[0];
            } else {
                for (int i = 0; i < customerStatuss.length; i++) {
                    if (i == (customerStatuss.length - 1)) {
                        customerStatus += "'" + customerStatuss[i];
                    } else if (i == 0) {
                        customerStatus += customerStatuss[i] + "'" + ",";
                    } else {
                        customerStatus += "'" + customerStatuss[i] + "'" + ",";
                    }
                }
            }
            params.put("customerStatus", " t2.customer_status in ('" + customerStatus + "')");
        }
        String tableHead = searchInfo.getAbbreviation();
        params.put("tableName", tableHead + "customer_info");
        params.put("tableName1", tableHead + "recharge_info");
        params.put("tableName2", tableHead + "deal_info");
        params.put("startIndex", searchInfo.getStartIndex());
        params.put("limitIndex", searchInfo.getLimitIndex());
        return dao.getQueryPage(page, params);
    }

    public int getQueryPagesCount(Page<PaymentRecordInfo> page, SearchPaymentRecordInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getCustomerName())) {
            params.put("customerName", searchInfo.getCustomerName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerPhone())) {
            params.put("customerPhone", searchInfo.getCustomerPhone());
        }
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCheckoutMethod())) {
            String checkoutMethod = "";
            String checkoutMethods[] = searchInfo.getCheckoutMethod().split(",");
            if (checkoutMethods.length == 1) {
                checkoutMethod = checkoutMethods[0];
            } else {
                for (int i = 0; i < checkoutMethods.length; i++) {
                    if (i == (checkoutMethods.length - 1)) {
                        checkoutMethod += "'" + checkoutMethods[i];
                    } else if (i == 0) {
                        checkoutMethod += checkoutMethods[i] + "'" + ",";
                    } else {
                        checkoutMethod += "'" + checkoutMethods[i] + "'" + ",";
                    }
                }
            }
            params.put("checkoutMethod", " t1.checkout_method in ('" + checkoutMethod + "')");
        }
        if (StringUtil.isNotEmpty(searchInfo.getFieldTc())) {
            String fieldTc = "";
            String fieldTcs[] = searchInfo.getFieldTc().split(",");
            if (fieldTcs.length == 1) {
                fieldTc = fieldTcs[0];
            } else {
                for (int i = 0; i < fieldTcs.length; i++) {
                    if (i == (fieldTcs.length - 1)) {
                        fieldTc += "'" + fieldTcs[i];
                    } else if (i == 0) {
                        fieldTc += fieldTcs[i] + "'" + ",";
                    } else {
                        fieldTc += "'" + fieldTcs[i] + "'" + ",";
                    }
                }
            }
            params.put("fieldTc", " (t1.tc_name in ('" + fieldTc + "') or d.tc_name in ('" + fieldTc + "'))");
        }
        if (StringUtil.isNotEmpty(searchInfo.getDoctor())) {
            params.put("doctor", searchInfo.getDoctor());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerStatus())) {
            String customerStatus = "";
            String customerStatuss[] = searchInfo.getCustomerStatus().split(",");
            if (customerStatuss.length == 1) {
                customerStatus = customerStatuss[0];
            } else {
                for (int i = 0; i < customerStatuss.length; i++) {
                    if (i == (customerStatuss.length - 1)) {
                        customerStatus += "'" + customerStatuss[i];
                    } else if (i == 0) {
                        customerStatus += customerStatuss[i] + "'" + ",";
                    } else {
                        customerStatus += "'" + customerStatuss[i] + "'" + ",";
                    }
                }
            }
            params.put("customerStatus", " t2.customer_status in ('" + customerStatus + "')");
        }
        String tableHead = searchInfo.getAbbreviation();
        params.put("tableName", tableHead + "customer_info");
        params.put("tableName1", tableHead + "recharge_info");
        params.put("tableName2", tableHead + "deal_info");
        return dao.getQueryPagesCount(page, params);
    }

    @Override
    public List<PaymentRecordInfo> getPaymentRecordExport(SearchPaymentRecordInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtil.isNotEmpty(searchInfo.getCustomerName())) {
            params.put("customerName", searchInfo.getCustomerName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerPhone())) {
            params.put("customerPhone", searchInfo.getCustomerPhone());
        }
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCheckoutMethod())) {
            String checkoutMethod = "";
            String checkoutMethods[] = searchInfo.getCheckoutMethod().split(",");
            if (checkoutMethods.length == 1) {
                checkoutMethod = checkoutMethods[0];
            } else {
                for (int i = 0; i < checkoutMethods.length; i++) {
                    if (i == (checkoutMethods.length - 1)) {
                        checkoutMethod += "'" + checkoutMethods[i];
                    } else if (i == 0) {
                        checkoutMethod += checkoutMethods[i] + "'" + ",";
                    } else {
                        checkoutMethod += "'" + checkoutMethods[i] + "'" + ",";
                    }
                }
            }
            params.put("checkoutMethod", " t1.checkout_method in ('" + checkoutMethod + "')");
        }
        if (StringUtil.isNotEmpty(searchInfo.getFieldTc())) {
            String fieldTc = "";
            String fieldTcs[] = searchInfo.getFieldTc().split(",");
            if (fieldTcs.length == 1) {
                fieldTc = fieldTcs[0];
            } else {
                for (int i = 0; i < fieldTcs.length; i++) {
                    if (i == (fieldTcs.length - 1)) {
                        fieldTc += "'" + fieldTcs[i];
                    } else if (i == 0) {
                        fieldTc += fieldTcs[i] + "'" + ",";
                    } else {
                        fieldTc += "'" + fieldTcs[i] + "'" + ",";
                    }
                }
            }
            params.put("fieldTc", " (t1.tc_name in ('" + fieldTc + "') or d.tc_name in ('" + fieldTc + "'))");
        }
        if (StringUtil.isNotEmpty(searchInfo.getDoctor())) {
            params.put("doctor", searchInfo.getDoctor());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerStatus())) {
            String customerStatus = "";
            String customerStatuss[] = searchInfo.getCustomerStatus().split(",");
            if (customerStatuss.length == 1) {
                customerStatus = customerStatuss[0];
            } else {
                for (int i = 0; i < customerStatuss.length; i++) {
                    if (i == (customerStatuss.length - 1)) {
                        customerStatus += "'" + customerStatuss[i];
                    } else if (i == 0) {
                        customerStatus += customerStatuss[i] + "'" + ",";
                    } else {
                        customerStatus += "'" + customerStatuss[i] + "'" + ",";
                    }
                }
            }
            params.put("customerStatus", " t2.customer_status in ('" + customerStatus + "')");
        }
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName1 = tableHead + "recharge_info";
        params.put("tableName", ShiroKit.getUser().getCustomer_info());
        params.put("tableName1", tableName1);
        params.put("tableName2", ShiroKit.getUser().getDeal_info());
        params.put("startIndex", searchInfo.getStartIndex());
        params.put("limitIndex", searchInfo.getLimitIndex());
        return dao.getPaymentRecordExport(params);
    }

}